var container = $("#acegiRoles");
var users=[];
var zTree = $("#usersTree");
var setting = {
	check: {
	    enable: true,
	    chkboxType :{ "Y" : "s", "N" : "s" }

	},
	async : {
		enable : true,
		url : "business/component!ztree",
		autoParam : ["id=bean.id"],
		otherParam:{"mapDefaultActionMethod":false}
	   // dataFilter: filter
	}
};

var zNodes = [
              {
            	  name : "西安庆华.组织机构",
          		  open : true,
          		  nocheck:true,
          		  id:"root",
          		  isParent:true
              }              
            ];
function filter(treeId, parentNode, childNodes) {
	if (!childNodes)
		return null;
	for ( var i = 0, l = childNodes.length; i < l; i++) {
		childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
	}
	return childNodes;
}
function beforeAsync(treeId, treeNode) {
	return true;
}

function initToolbars() {
    var tools=[];
	var t = {
			type : 'button',
			text : "保存",
			bodyStyle : 'new',
			useable : 'T',
			handler : function() {							
			    saveAppendUsers();	
			}
		};
	tools.push(t);
	var toolbar = new Toolbar({
		renderTo : 'toolbar_a',
		// border: 'top',
		items : tools

	});
	toolbar.render();

}
function saveAppendUsers(){
	var data = "";
	data+="bean.name="+container.val();
	var mytree  =$.fn.zTree.getZTreeObj("usersTree");
	var nodes = mytree.getCheckedNodes(true);
	for(var i=0;i<nodes.length;i++){
		var n = nodes[i];
		data+="&users["+i+"].username="+n.id;		
	}
	var url = 'admin/securole/secu-role!appenduser'
	doSubmit(url,data);
}
$(function() {
	
	$.getJSON( 
	    "admin/securole/secu-role!gets",
		function(data,textStatus,jqXHR) {
			$.each(data, function(idx, item) {
				var opt = $("<option></option>");
				opt.val(item.name);
				opt.text(item.description);
				if(acegi==item.name)opt.attr("selected",true);
				container.append(opt);
			  }
			);
		}
	);
	//initToolbars() 
	$("#saveAppend").bind("click",function(){
		saveAppendUsers();	
	}).button();
	$.fn.zTree.init(zTree , setting ,zNodes);

});